package com.test.BlockingQueueTest;

import org.json.JSONArray;
import org.json.JSONObject;


/**
 * Created by yang on 17/3/27.
 */
public class StuDoExam {

    public static String startExam(String token) throws Exception {
        String path = Basic.stuApiRoot + "/homework/exam/start";
        String url = path + "?token=" + token + "&examId=" + Test1.doId;
//        String url = path + "?token=" + token + "&examId=" + "39747";
        return ConnectionTest.sendGet(url);
    }

    // 实现将question-list接口中的作业题目和作业答案分别存放在list中
    public static void getExamAnswer(String op) throws Exception {
        if (op != null && !op.isEmpty()){
            JSONObject jsonObject = new JSONObject(op);
            String data = jsonObject.optString("data");
            JSONObject dataJson = new JSONObject(data);
            JSONArray jsonArray = dataJson.optJSONArray("questionList");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject quesJson = jsonArray.getJSONObject(i);
                Basic.questionId.add(quesJson.optString("questionID"));
                Basic.rightAnswer.add(quesJson.optString("rightAnswer"));
            }
        }
    }

    public static void submitExam(String token) throws Exception {
        String path = Basic.stuApiRoot + "/homework/exam/submit";
        JSONArray jsonArray = new JSONArray();
        int random = Math.random() > 0.8 ? 1 : 0;
        if ( random == 1 ){
            for (int i = 0; i < Basic.questionId.size(); i++) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("questionID", Basic.questionId.get(i));
                jsonObject.put("answer", Basic.rightAnswer.get(i));
                jsonObject.put("spendTime", "3000");
                jsonArray.put(jsonObject);

            }
        }else {
            for (int i = 0; i < Basic.questionId.size(); i++) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("questionID", Basic.questionId.get(i));
                int random1 = Math.random() > 0.3 ? 1 : 0;
                if ( random1 == 1 ){
                    jsonObject.put("answer", Basic.rightAnswer.get(i));
                }else {
                    jsonObject.put("answer", "B");
                }
                jsonObject.put("spendTime", "3000");
                jsonArray.put(jsonObject);

            }
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("token", token);
        jsonObject.put("examId", Test1.doId);
        jsonObject.put("list", jsonArray);
        jsonObject.put("isAutoSubmit", "0");

        ConnectionTest.sendPost(path, jsonObject.toString());
        Basic.questionId.clear();
        Basic.rightAnswer.clear();

    }

    public static void main(String[] args) throws Exception {
        String token = "W/ugdq2LhzHICQTxgZsbFN6x3U5flA1Wnd6+0nH+/wdf+gWH1erL9IypNye1q5oS";
//        System.out.println((getJiaoCai(token)));
        getExamAnswer(startExam(token));

    }


}
